package com.yixin.hospital.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class CaffeineConfig {

    /**
     * 过期时间
     **/
    public static long tokenExpirationSec;

    @Value("${token.expiration.sec}")
    public void setTokenExpirationSec(long tokenExpirationSec) {
        CaffeineConfig.tokenExpirationSec = tokenExpirationSec;
    }

    @Bean
    public Cache<String, Object> userCaffeineCache() {
        return Caffeine.newBuilder()
                .expireAfterWrite(Duration.ofSeconds(tokenExpirationSec))
                .build();
    }

    @Bean
    public Cache<String, Object> userTokenCaffeineCache() {
        return Caffeine.newBuilder()
                .expireAfterWrite(Duration.ofSeconds(tokenExpirationSec))
                .build();
    }
}

